package com.haoxuer.imake.fields;

import com.haoxuer.discover.data.annotations.FormAnnotation;
import com.haoxuer.discover.data.annotations.FormFieldAnnotation;
import java.util.Collections;
import java.util.Comparator;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.util.List;

public class FormBeanConver {

    public FormBean converClass(Class<?> object) {
        FormBean formBean = new FormBean();
        List<FieldBean> beanList = formBean.getFields();
        FormAnnotation anno = object.getAnnotation(FormAnnotation.class);
        if (anno != null) {
            formBean.setTitle(anno.title());
            formBean.setAdd(anno.add());
            formBean.setUpdate(anno.update());
            formBean.setList(anno.list());
        }
        for (Class<?> clazz = object; clazz != Object.class; clazz = clazz.getSuperclass()) {
            Field[] fs = clazz.getDeclaredFields();
            for (Field f : fs) {
                f.setAccessible(true);
                if (f.getName().equals("id")) {
                    continue;
                }
                FormFieldAnnotation annotation = f.getAnnotation(FormFieldAnnotation.class);
                FieldBean bean = new FieldBean();
                if (annotation == null) {
                    continue;
                }
                bean.setClassName(annotation.className());
                bean.setId(annotation.id());
                if (bean.getId() == null) {
                    bean.setId(f.getName());
                }
                bean.setType(annotation.type().name());
                bean.setPlaceholder(annotation.placeholder());
                Integer sortNum = getInteger(annotation);
                bean.setSortNum(sortNum);
                bean.setTitle(annotation.title());
                bean.setCol(annotation.col()+"");
                if (annotation.grid()) {
                    formBean.getGrids().add(bean);
                }
                if (StringUtils.isEmpty(bean.getTitle())) {
                    bean.setTitle(f.getName());
                }
                if (StringUtils.isEmpty(bean.getId())) {
                    bean.setId(f.getName());
                }
                if (StringUtils.isEmpty(bean.getPlaceholder())) {
                    bean.setPlaceholder(bean.getTitle());
                }
                if ("date".equals(bean.getType())) {
                    formBean.setHasDate(true);
                    formBean.getDates().add(bean);
                }
                if ("image".equals(bean.getType())) {
                    formBean.setHasImage(true);
                    formBean.getImages().add(bean);
                }
                if (annotation.ignore()) {
                    continue;
                }
                beanList.add(bean);
            }
        }
        beanList.sort(new Comparator<FieldBean>() {
            @Override
            public int compare(FieldBean o1, FieldBean o2) {
                return o1.getSortNum().compareTo(o2.getSortNum());
            }
        });
        Collections.sort(formBean.getGrids());
        return formBean;
    }

    public FormBean conver(Object object) {
        return converClass(object.getClass());
    }

    private Integer getInteger(FormFieldAnnotation annotation) {
        Integer result = 0;
        String b = annotation.sortNum();
        try {
            result = Integer.parseInt(b);
        } catch (Exception e) {
            result = 0;
        }
        return result;
    }
}
